
const SocketDatas = require('./SocketDatas')
const moment= require('moment');



//保存
const saveSocketDatas=async(uid,is_active,reason,socket_id)=> {

    let time=moment().format('YYYY-MM-DD  h:mm:ss a');

    let obj={uid,is_active,reason,socket_id,time}
    let socket=new SocketDatas(obj)
    await socket.save();

    return obj
}
exports.saveSocketDatas=saveSocketDatas


//获取
const getSockets=async(uid)=> {

    let data=await SocketDatas.findOne({uid:uid-0})

    return data
}
exports.getSockets=getSockets



//更新
exports.updataSocket=async(uid,is_active,reason,socket_id)=> {

    let sockets=await getSockets(uid)

    if(!sockets){
        saveSocketDatas(uid,is_active,reason,socket_id)
    }
    else{

        let time=moment().format('YYYY-MM-DD  h:mm:ss a');

        let new_sockets=Object.assign(sockets,{is_active:is_active,reason:reason,socket_id:socket_id,time})
        await SocketDatas.update({uid:uid-0},new_sockets)
    }
}



//拿到socket列表
exports.getSocketList=async()=> {

    let data=await SocketDatas.find().sort('uid')

    return data
}



exports.getSocketListNum=async()=> {

    let data=await SocketDatas.find().sort('uid').count()

    return data
}
